// pages/information/information.js
import {
  API_CONFIRM_BINDING_PERSONNEL
} from '../../api/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showData: [
      {
        title: '姓名',
        value: '',
        key: 'username'
      },
      {
        title: '身份证号',
        value: '',
        key: 'idcardno'
      },
      {
        title: '省市',
        value: '',
        key: 'provincename_cityname'
      },
      {
        title: '学校',
        value: '',
        key: 'schoolname'
      },
      {
        title: '分类',
        value: '',
        key: 'type'     // 分类 学生1 职工2 访客3
      },
      {
        title: '学号',
        value: '',
        key: 'personid'
      },
      {
        title: '联系电话',
        value: '',
        key: 'phone'
      },
      {
        title: '班级',
        value: '',
        key: 'classname'
      },
      {
        title: '性别',
        value: '',
        key: 'sex'
      },
      {
        title: '班主任',
        value: '',
        key: 'headmaster'
      },
      {
        title: '班主任联系方式',
        value: '',
        key: 'headmasterphone'
      },
      {
        title: '家长',
        value: '',
        key: 'parentname'
      },
      {
        title: '家长联系电话',
        value: '',
        key: 'parentphone'
      },
      {
        title: '家庭住址',
        value: '',
        key: 'address'
      },
    ],
    userWenGanYunData: [] // 返回的数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const userWenGanYunInfo = wx.getStorageSync('userWenGanYunInfo')
    const arrData = []
    this.data.showData.forEach(item => {
      let value = userWenGanYunInfo[item.key]
      if (item.key == 'provincename_cityname') {
        value = userWenGanYunInfo.provincename + userWenGanYunInfo.cityname
      }
      if (value) {
        if (item.key == 'sex') {
          value = value == 1 ? '男' : '女'
        }
        if (item.key == 'type') {
          const types = {
            1: '学生',
            2: '职工',
            3: '访客'
          }
          value = types[value]
        }
        arrData.push({
          ...item,
          value
        })
      }
    })
    this.setData({
      userWenGanYunData: [...arrData]
    })
  },

  // 信息确认
  onTrueInfo() {
    const { username, idcardno, current_type } = wx.getStorageSync('userWenGanYunInfo')
    const params = {
      open_id: wx.getStorageSync('openId'),
      username,
      idcardno,
      current_type: wx.getStorageSync('changeSystem') ? '1' : current_type
    }
    API_CONFIRM_BINDING_PERSONNEL(params).then(res => {
      wx.setStorageSync('changeSystem', false)
      // 跳转首页
      wx.reLaunch({
        url: '/pages/index/index',
      })
    })
    
  }
})